package mirkofrancuski.battleship.db;

import java.util.List;

import mirkofrancuski.battleship.game.Board;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 2;
	private static final String DATABASE_NAME = "Battleship_Database";

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		HighScoreTable.onCreate(db);
		BoardTable.onCreate(db, BoardTable.BOARD_PLACEMENT);
		BoardTable.onCreate(db, BoardTable.BOARD_GAME_PLAYER_MAIN);
		BoardTable.onCreate(db, BoardTable.BOARD_GAME_PLAYER_SECUNDARY);
		BoardTable.onCreate(db, BoardTable.BOARD_GAME_COMPUTER_MAIN);
		BoardTable.onCreate(db, BoardTable.BOARD_GAME_COMPUTER_SECUNDARY);
		CoordinateTable.onCreate(db);
		ProbabilityMatrixTable.onCreate(db);
		IntTable.onCreate(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		HighScoreTable.onUpgrade(db, oldVersion, newVersion);
		BoardTable.onUpgrade(db, oldVersion, newVersion, BoardTable.BOARD_PLACEMENT);
		BoardTable.onUpgrade(db, oldVersion, newVersion, BoardTable.BOARD_GAME_PLAYER_MAIN);
		BoardTable.onUpgrade(db, oldVersion, newVersion, BoardTable.BOARD_GAME_PLAYER_SECUNDARY);
		BoardTable.onUpgrade(db, oldVersion, newVersion, BoardTable.BOARD_GAME_COMPUTER_MAIN);
		BoardTable.onUpgrade(db, oldVersion, newVersion, BoardTable.BOARD_GAME_COMPUTER_SECUNDARY);
		CoordinateTable.onUpgrade(db, oldVersion, newVersion);
		ProbabilityMatrixTable.onUpgrade(db, oldVersion, newVersion);
		IntTable.onUpgrade(db, oldVersion, newVersion);
	}

	public boolean addHighScore(HighScoreInfo highScoreInfo) {
		SQLiteDatabase db = this.getWritableDatabase();

		boolean returnValue = HighScoreTable.addHighScore(db, highScoreInfo);

		db.close();
		return returnValue;
	}

	public List<HighScoreInfo> getAllHighScores() {

		SQLiteDatabase db = this.getReadableDatabase();

		List<HighScoreInfo> returnList = HighScoreTable.getAllHighScores(db);

		db.close();
		return returnList;
	}

	public void savePlacementBoard(Board board) {
		SQLiteDatabase db = this.getWritableDatabase();

		BoardTable.saveBoard(db, board, BoardTable.BOARD_PLACEMENT);

		db.close();
	}

	public Board restorePlacementBoard() {
		SQLiteDatabase db = this.getWritableDatabase();

		Board board = BoardTable.restoreBoard(db, BoardTable.BOARD_PLACEMENT);

		db.close();

		return board;
	}

	public void deletePlacementBoard() {
		SQLiteDatabase db = this.getWritableDatabase();

		BoardTable.deleteBoard(db, BoardTable.BOARD_PLACEMENT);

		db.close();

	}

}
